import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Parse by looking for the string <a
 * href="/homeinspectors/California/Fallbrook.aspx">
 * 
 * @author mkvalsvik
 * 
 */
public class AshiInspectorDetailParser {

	private static Logger logger = LoggerFactory
			.getLogger(AshiInspectorDetailParser.class.getName());

	private static final String INPUT_DIR = "/mads/hobby/it/data/inspectors";

	private StringBuilder sb = null;

	public static void main(String[] args) {
		logger.debug("Starting..");

		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(
					"extractedInspectorDetails.tsv"));

			AshiInspectorDetailParser instance = new AshiInspectorDetailParser();
			File dir = new File(INPUT_DIR);			
			instance.parseDir(dir, writer);
			
			/*
			File file = new File(dir, "httpwwwashiorghomeinspectorsAlabamaTuscaloosa17408JBaspx.html");
			instance.parseFile(file, writer);
			*/

		} catch (IOException e1) {
			logger.error("Error", e1);
		} catch (ParserException e) {
			logger.error("Error", e);
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				// ignore
			}
		}
	}

	public void parseDir(File dir, BufferedWriter writer)
			throws ParserException, IOException {

		File[] files = dir.listFiles();
		for (int i = 0; i < files.length; i++) {

			// Read into a string
			sb = new StringBuilder();
			if (files[i].getName().endsWith(".html")) {
				parseFile(files[i], writer);
				//return;
			}
		}
	}

	private char[] cbuf = new char[1024];

	public void parseFile(File file, BufferedWriter writer) throws IOException,
			ParserException {

		logger.debug("Parsing " + file.getAbsolutePath());
		char[] cbuf = new char[1024];

		// Read into a string
		String content = FileUtils.readFileToString(file);
		Parser parser = new Parser();
		parser.setInputHTML(content);
		parser.setEncoding("utf-8");
		NodeList nodeList = parser.parse(null);

		NodeList divNodes = nodeList.extractAllNodesThatMatch(
				new HasAttributeFilter("class", "contentMain"), true);
		if (divNodes.size() < 1) {
			logger.warn(String.format("%s: Found no div named contentMain %d",
					file.getAbsolutePath(), divNodes.size()));
			return;
		}

		// Second div
		Div divTag = (Div) divNodes.elementAt(1);
		NodeList tableNodes = divTag.getChildren().extractAllNodesThatMatch(
				new TagNameFilter("table"), true);
		if (tableNodes.size() < 1) {
			logger.warn(String.format(
					"%s: Found no table inside contentMain %d", file
							.getAbsolutePath(), tableNodes.size()));
			return;
		}

		TableTag tableTag = (TableTag) tableNodes.elementAt(0);
		TableRow row1 = tableTag.getRow(0);
		TableColumn row1Col1 = row1.getColumns()[0];
		System.out.println(row1Col1.toHtml());

		String s = row1Col1.toHtml();
		SimpleHtmlParser p = new SimpleHtmlParser(s);
		p.skipTag(); // <td
		p.skipTag(); // <b>
		String name = p.extractUntilTag();
		System.out.println("name: " + name);

		p.skipTag();
		String company = p.extractUntilTag();
		System.out.println("company: " + company);
		
		// Was company missing?  
		if (company.indexOf("nbsp") > 0) {
			
			// Yes
			company = null;
			p.undoLast();
		}

		String city = p.extractUntilSubstringOrTag(",");
		System.out.println("city: " + city);

		p.advance(" ");
		String stateCode = p.extractNChars(2);
		System.out.println("stateCode: " + stateCode);

		// zip
		p.advance(6); // past &nbsp;
		String zipCode = p.extractNChars(5);
		System.out.println("zipCode: " + zipCode);

		// phone
		p.advance(4);
		String phone = p.extractUntilTag();
		System.out.println("phone: " + phone);

		// cell
		String cell = null;
		if (p.hasString("Cell")) {
			p.advance("Cell: ");
			cell = p.extractUntilTag();
		}

		String url = p.extractHref();
		
		String line = String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
				name, company, city, stateCode, zipCode, phone, cell, url);
		System.out.println(line);		
		
		if (p.skipToEndOfString("License Number:")) {
			String license = p.extractUntilTag().trim();
		}
		p.skipToEndOfString("Additional services offered :");
		p.skipToNamedTag("ul");
		int servicesEndTag = p.getCurrentPosition() + s.substring(p.getCurrentPosition()).indexOf("</ul>");
		StringBuilder services = new StringBuilder();
		do {
			String service = p.extractUntilTag();
			services.append(service);
			services.append("\t");
			System.out.println("Service: " + service);
		} while (p.getCurrentPosition() < servicesEndTag);
		
		writer.write(line + "\t" + services.toString());
		writer.newLine();

		
	}

	public AshiInspectorDetailParser() {
	}

}
